// ManualConnect.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "ManualConnect.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CManualConnect property page
#define ID_CONNECTION_STATUS		1		//timer id
#define CONNECTION_DETECT_INTERVAL	2000	//2 sec

IMPLEMENT_DYNCREATE(CManualConnect, CPropertyPage)

CManualConnect::CManualConnect() : CPropertyPage(CManualConnect::IDD)
{
	//{{AFX_DATA_INIT(CManualConnect)
	m_UnitIDName = _T("");
	//}}AFX_DATA_INIT
}

CManualConnect::~CManualConnect()
{
}

void CManualConnect::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CManualConnect)
	DDX_Text(pDX, IDC_FixedIDName, m_UnitIDName);
	DDV_MaxChars(pDX, m_UnitIDName, 14);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CManualConnect, CPropertyPage)
	//{{AFX_MSG_MAP(CManualConnect)
	ON_BN_CLICKED(IDC_Connect, OnConnect)
	ON_BN_CLICKED(IDC_Disconnect, OnDisconnect)
	ON_BN_CLICKED(IDC_ChangeID, OnChangeID)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_AutoScan, OnAutoScan)
	ON_BN_CLICKED(IDC_FactoryReset, OnFactoryReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CManualConnect message handlers

BOOL CManualConnect::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	if (!m_ManualConnect.Create(this))
		g_GlobalObj.UIWriteLog("m_ManualConnect.Create Error");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CManualConnect::OnConnect() 
{
	CString szMsg;

	if (!m_ManualConnect.Connect())
	{
		szMsg.LoadString(IDS_ConnectFail);
	}
	else
	{
		szMsg.LoadString(IDS_SetupConnectionSuccess);
		SetTimer(ID_CONNECTION_STATUS, CONNECTION_DETECT_INTERVAL, NULL);
	}
	SetFocus();
	AfxMessageBox(szMsg);
}

void CManualConnect::OnDisconnect() 
{
	if (!m_ManualConnect.Disconnect())
		g_GlobalObj.UIWriteLog("m_ManualConnect.Disconnect Error");
	KillTimer(ID_CONNECTION_STATUS);	
}

void CManualConnect::OnChangeID() 
{
	if (!m_ManualConnect.ChangeUnitID())
		g_GlobalObj.UIWriteLog("m_ManualConnect.ChangeUnitID Error");
	
}
/*
void CManualConnect::OnChangeConnectPassword() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CPropertyPage::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	if (!m_ManualConnect.ConnectPasswdChanged())
		g_GlobalObj.UIWriteLog("m_ManualConnect.ConnectPasswdChanged Error");
	
}
*/
BOOL CManualConnect::OnKillActive() 
{
/*	if(!m_ManualConnect.UpdateMemberVariables())
		g_GlobalObj.UIWriteLog("m_ManualConnect.UpdateMemberVariables Error");
*/
//	KillTimer(ID_CONNECTION_STATUS);
	
	return CPropertyPage::OnKillActive();
}

BOOL CManualConnect::OnSetActive() 
{
	if(!m_ManualConnect.UpdateUI())
		g_GlobalObj.UIWriteLog("m_ManualConnect.UpdateUI Error");

	return CPropertyPage::OnSetActive();
}

void CManualConnect::OnTimer(UINT nIDEvent) 
{
	CString			szMsg;

	if (!g_GlobalObj.ContinuousUpdate()) //3 read error means error
	{
		KillTimer(ID_CONNECTION_STATUS);
		szMsg.LoadString(IDS_ConnectionBroken);
		AfxMessageBox(szMsg);
	}

	CPropertyPage::OnTimer(nIDEvent);
}

void CManualConnect::OnAutoScan() 
{
	CString szMsg;

	if (!m_ManualConnect.AutoScan())
	{
		szMsg.LoadString(IDS_ConnectFail);
	}
	else
	{
		szMsg.LoadString(IDS_SetupConnectionSuccess);
		SetTimer(ID_CONNECTION_STATUS, CONNECTION_DETECT_INTERVAL, NULL);
	}
	SetFocus();
	AfxMessageBox(szMsg);
}

void CManualConnect::OnFactoryReset() 
{
	CString szMsg;
	int iRet;

	szMsg.LoadString(IDS_FactoryResetWarning);
	iRet = AfxMessageBox(szMsg ,MB_OKCANCEL);
	if (iRet == IDCANCEL)
		return ;

	if (!m_ManualConnect.FactoryReset())
	{
		szMsg.LoadString(IDS_FactoryResetFail);
	}
	else
	{
		szMsg.LoadString(IDS_Successful);
		KillTimer(ID_CONNECTION_STATUS);
	}	
	AfxMessageBox(szMsg);	
}
